package com.wavehi.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 22/9/2021 上午 11:05
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@WebServlet("/sessionDemoServlet")
public class SessionDemoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取会话对象
        HttpSession session = req.getSession();
        //获取会话id
        System.out.println("SessionId is : " + session.getId());
        //获取Servlet的上下文对象
        System.out.println("SessionServletContext : " + session.getServletContext());
        //设置会话对象的数据
        session.setAttribute("username", "Marry");
        //获取会话请求的间隔时间
        System.out.println("SessionInactiveAge : " + session.getMaxInactiveInterval());
        //设置会话请求的间隔时间
        session.setMaxInactiveInterval(20);
        //使会话失效(去除所有绑定的数据
        session.invalidate();
        String id = session.getId();
//        System.out.println("username : " + session.getAttribute("username"));//失效的会话不可使用
    }
}
